<!-- =====================================
作者：老曹  QQ:1484743
版权声明：本程序为个人原创、开源程序，禁止用于任何违法用途，任何违法用途均与作者无关。
仅限学习交流，转载请注明出处。如需商用请联系作者。
===================================== -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>一键生成宿主机监控脚本</title>
    <link rel="stylesheet" href="bootstrap.min.css">
    <style>
        body { background: #f8f9fa; }
        .center-card { max-width: 500px; margin: 60px auto 0; box-shadow: 0 4px 24px rgba(0,0,0,0.10); border-radius: 16px; }
        .card-body { padding: 2rem; }
        .form-label { font-weight: bold; }
        #result pre { background: #f4f4f4; border-radius: 8px; padding: 1em; }
        #result { margin-top: 2em; }
        .password-mask { position:fixed;top:0;left:0;width:100vw;height:100vh;background:rgba(255,255,255,0.98);z-index:9999;display:flex;align-items:center;justify-content:center; }
        .password-box { background:#fff;box-shadow:0 2px 16px rgba(0,0,0,0.12);border-radius:12px;padding:2em 2em 1.5em 2em;text-align:center; }
    </style>
</head>
<body>
<div class="password-mask" id="pw-mask">
    <div class="password-box">
        <h5>请输入访问密码</h5>
        <input type="password" id="pw-input" class="form-control mb-3" placeholder="请输入密码">
        <button class="btn btn-primary w-100" onclick="checkPw()">进入</button>
        <div id="pw-tip" style="color:#db2828;margin-top:10px;display:none;">密码错误！</div>
    </div>
</div>
<div class="card center-card">
    <div class="card-body">
        <h3 class="mb-4 text-center">一键生成宿主机监控脚本</h3>
        <form id="gen-form" style="position:relative;">
            <div class="form-group mb-3">
                <label for="name" class="form-label">主机名称</label>
                <input type="text" id="name" class="form-control" required>
            </div>
            <div class="form-group mb-3">
                <label for="ip" class="form-label">IP</label>
                <input type="text" id="ip" class="form-control" required>
            </div>
            <div class="form-group mb-4">
                <label for="os" class="form-label">操作系统</label>
                <select id="os" class="form-control">
                    <option value="linux">Linux</option>
                    <option value="windows">Windows</option>
                    <option value="macos">macOS</option>
                </select>
            </div>
            <button type="submit" id="gen-btn" class="btn btn-success w-100 mb-2">自动生成脚本</button>
            <button type="button" class="btn btn-warning w-100" id="regen-btn" style="display:none;">再次生成</button>
        </form>
        <div id="result" style="display:none;">
            <h5 class="mt-4">一键命令：</h5>
            <pre id="cmd"></pre>
            <button class="btn btn-outline-primary mb-3 w-100" id="copy-cmd-btn">复制一键命令</button>
            <h5>脚本内容：</h5>
            <pre id="script"></pre>
        </div>
    </div>
</div>
<script src="jquery.min.js"></script>
<script>
function checkPw(){
    var pw = document.getElementById('pw-input').value;
    if(pw === '8465'){
        var now = Date.now();
        localStorage.setItem('gen_script_auth', now);
        document.getElementById('pw-mask').style.display='none';
    }else{
        document.getElementById('pw-tip').style.display='block';
    }
}
// 自动检测24小时内是否已验证
window.onload = function(){
    var last = localStorage.getItem('gen_script_auth');
    if(last && (Date.now() - parseInt(last)) < 24*60*60*1000){
        document.getElementById('pw-mask').style.display='none';
    }
}
$('#gen-form').on('submit', function(e){
    e.preventDefault();
    var name = $('#name').val();
    var ip = $('#ip').val();
    var os = $('#os').val();
    if(!name || !ip || !os) return;
    var $btn = $('#gen-btn');
    $btn.prop('disabled', true).text('生成中...');
    $.ajax({
        url: '/api/host_register.php',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify({name: name, ip: ip, os: os}),
        success: function(res){
            if(res.hostId){
                var hostId = res.hostId;
                $.get('/api/script_generate.php?hostId=' + hostId, function(data){
                    var filename = 'monitor_' + hostId + '.sh';
                    $.post('/save_script.php', {filename: filename, content: data}, function(saveRes){
                        if(saveRes.success){
                            var url = '你的API地址/' + filename;
                            var cmd = 'curl -O -k ' + url + ' && chmod +x ' + filename + ' && ./' + filename;
                            $('#cmd').text(cmd);
                            $('#script').text(data + '\n\n# 脚本已自动检测并监控所有硬盘');
                            $('#result').show();
                            $btn.hide();
                            $('#regen-btn').show().off('click').on('click', function(){
                                $('#gen-form')[0].reset();
                                $('#result').hide();
                                $btn.prop('disabled', false).removeClass('btn-warning').addClass('btn-success').text('自动生成脚本').show();
                                $(this).hide();
                            });
                            $('#copy-cmd-btn').off('click').on('click', function(){
                                var cmdText = $('#cmd').text();
                                if(navigator.clipboard){
                                    navigator.clipboard.writeText(cmdText);
                                }else{
                                    var ta = document.createElement('textarea');
                                    ta.value = cmdText;
                                    document.body.appendChild(ta);
                                    ta.select();
                                    document.execCommand('copy');
                                    document.body.removeChild(ta);
                                }
                                $(this).text('已复制!');
                                setTimeout(()=>{$(this).text('复制一键命令');}, 1500);
                            });
                        }else{
                            alert('保存脚本失败：' + saveRes.msg);
                            $btn.prop('disabled', false).removeClass('btn-warning').addClass('btn-success').text('自动生成脚本');
                            $('#regen-btn').hide();
                        }
                    }, 'json');
                });
            }else{
                alert('主机注册失败！');
                $btn.prop('disabled', false).removeClass('btn-warning').addClass('btn-success').text('自动生成脚本');
                $('#regen-btn').hide();
            }
        },
        error: function(){
            alert('主机注册接口请求失败！');
            $btn.prop('disabled', false).removeClass('btn-warning').addClass('btn-success').text('自动生成脚本');
            $('#regen-btn').hide();
        }
    });
});
</script>
</body>
</html> 